﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using SportMeetingPlanner.Models;

namespace SportMeetingPlanner.Controllers
{
    public class SportManagerController : Controller
    {
        private SportMeetingEntities db = new SportMeetingEntities();

        //
        // GET: /SportManager/
        public ActionResult Index()
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            return View(db.Sports.OrderBy(s => s.Name).ToList());
        }

        //
        // GET: /SportManager/Details/5
        public ActionResult Details(int id)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            Sport sport = db.Sports.Find(id);
            return View(sport);
        }

        //
        // GET: /SportManager/Create
        public ActionResult Create()
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        //
        // POST: /SportManager/Create
        [HttpPost]
        public ActionResult Create(Sport sport)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            if (ModelState.IsValid)
            {
                db.Sports.Add(sport);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(sport);
        }

        //
        // GET: /SportManager/Edit/5
        public ActionResult Edit(int id)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            Sport sport = db.Sports.Find(id);
            return View(sport);
        }

        //
        // POST: /SportManager/Edit/5
        [HttpPost]
        public ActionResult Edit(Sport sport)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            if (ModelState.IsValid)
            {
                db.Entry(sport).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sport);
        }

        //
        // GET: /SportManager/Delete/5
        public ActionResult Delete(int id)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            Sport sport = db.Sports.Find(id);
            return View(sport);
        }

        //
        // POST: /SportManager/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = GetAuthenticatedUser();
            if (user == null || !user.IsAdmin)
            {
                return RedirectToAction("Index", "Home");
            }

            Sport sport = db.Sports.Find(id);
            db.Sports.Remove(sport);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private User GetAuthenticatedUser()
        {
            User result = null;

            if (User.Identity.IsAuthenticated)
            {
                result = db.Users.Find(User.Identity.Name);
            }

            return result;
        }
    }
}